#include <iostream>
#include <mysql.h>

using namespace std;

const char* serverip = "127.0.0.1";
const char* user = "hyk";
const char* password = "2593hyK.";
const char* db = "libary_managment";


int main()
{
    MYSQL* conn;
    conn = mysql_init(NULL);
    
    if(!mysql_real_connect(conn, serverip, user, password, db, 0, NULL, 0))
    {
        cout << "error~" << endl;
        return 1;
    }
    mysql_set_character_set(conn, "utf8");

    string query = "select * from books";
    int n = mysql_query(conn, query.c_str());
    if(n != 0){
        printf("query error ~\n");
        return 2;
    }
    MYSQL_RES* res = mysql_store_result(conn);
    int fields = mysql_num_fields(res);
    MYSQL_FIELD *field = mysql_fetch_fields(res);
    int i = 0;
    for(; i < fields; i++){
        cout<<field[i].name<<" ";
    }
    cout << endl;
    i = 0;
    MYSQL_ROW line;
    for(; i < 2; i++){
        line = mysql_fetch_row(res);
        int j = 0;
        for(; j < fields; j++){
            cout<<line[j]<<" ";
        }
        cout<<endl;
    }
    return 0;

}